// stores/counter.js
import request from "../utils/request"
import { defineStore } from 'pinia'
import { treeDataTranslate } from "../utils/treeDataTranslate"
import { ElMessage } from "element-plus"
export const cole = defineStore('cole', {
    state: () => {
        return {
            role_list: [],//页面数据
            role_list_Len: 0,//页面总数据的长度
            role_flag_list: [],//弹框中的列表数据
            edit_flag_obj: <any>{},//编辑弹框数据
        }
    },

    actions: {
        //获取页面数据
        get_list() {
            request({

                url: `/sys/role/page?t=${new Date().getTime()}&current=1&size=10`,
                method: "get",
            }).then((res) => {
                console.log(res);

                this.role_list = res.data.records
                this.role_list_Len = res.data.total
                console.log(this.role_list, "this.role_list");

            });
        },
        //获取弹框中的数据
        flag_list() {
            request({
                url: `/sys/menu/table?t=${new Date().getTime()}`,
                method: "get"
            }).then(res => {

                this.role_flag_list = treeDataTranslate(res.data, "menuId", "parentId")
                console.log(this.role_flag_list, "res");
            })
        },
        //点击确认添加数据
        add_list(obj: any) {
            request({
                url: "/sys/role",
                method: "post",
                data: {
                    remark: obj.remark,
                    roleName: obj.roleName,
                    menuIdList: obj.menuIdList,
                    t: new Date().getTime()
                }
            }).then(res => {
                console.log(res, "res");
                this.get_list()
            })
        },
        //编辑弹框默认选中的数据id
        edit_flag(id: any) {
            request({
                url: `/sys/role/info/${id}?t=${new Date().getTime()}`,
                method: "get",
            }).then(res => {
                this.edit_flag_obj = res.data
                console.log(res, "res");


            })
        },
        //点击确认编辑更换数据
        edit_list(obj: any) {
            request({
                url: "/sys/role",
                method: "put",
                data: {
                    menuIdList: obj.menuIdList,
                    remark: obj.remark,
                    roleName: obj.roleName,
                    roleId: obj.roleId
                }
            }).then(res => {
                console.log(res, "res");
                this.get_list()
            })
        },
        search_lst(username: string) {
            request({
                url: `/sys/role/page?t=${new Date().getTime()}&current=1&size=10&roleName=${username}`,
                method: "get"
            }).then(res => {
                console.log(res, "res");
                this.role_list = res.data.records
            })
        },
        del_list(obj: any) {
            request({
                url: `/sys/role`,
                method: "delete",
                data: [obj.roleId]
            }).then(res => {
                console.log(res, "res");
                ElMessage({
                    type: "success",
                    message: "删除成功",
                });
                this.get_list()
            })
        }
    },

})